/**
 * 
 */
package com.igatewebsol.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.igatewebsol.action.UserTypeBean;
import com.igatewebsol.actionforms.HotelUserBean;
import com.igatewebsol.dao.UserDAO;

/**
 * @author Aravind Rapuru
 *
 */
public class UserDAOImpl extends BaseDAOImpl implements UserDAO{

	public int createUser(HotelUserBean userBean) {
		Connection con = null;
		PreparedStatement ps = null;
		String inserthotelUser = "insert into hotel_user_table values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		int insertcount = 0;
		try {
			con = getDBConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement(inserthotelUser,Statement.RETURN_GENERATED_KEYS);
			ps.setInt(1, 0);
			ps.setString(2, userBean.getUserpass());
			ps.setString(3, userBean.getUsertype());
			ps.setString(4,userBean.getUserfirstname());
			ps.setString(5, userBean.getUsermiddlename());
			ps.setString(6, userBean.getUserlastname());
			ps.setString(7, userBean.getUseremailid());
			ps.setInt(8, userBean.getUsermobileno());
			ps.setInt(9, userBean.getUserlandline());
			ps.setString(10, userBean.getUserAddresslineone());
			ps.setString(11, userBean.getUserAddresslinetwo());
			ps.setString(12, userBean.getUserAddresslinethree());
			ps.setString(13, userBean.getUsercity());
			ps.setString(14, userBean.getUserstate());
			ps.setString(15, userBean.getUsercountry());
			ps.setInt(16, userBean.getUserpincode());
			insertcount = ps.executeUpdate();
			con.commit();
		} catch (Exception e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return insertcount;
	}
	
	public int editUser(HotelUserBean userBean) {
		Connection con = null;
		PreparedStatement ps = null;
		String edithotelUser = "update hotel_user_table set user_type=?, user_firstname=?, user_middlename=?, user_lastname=?, user_emailid=?, user_mobileno=?, user_landline=?, user_addrlineone=?, user_addrlinetwo=?, user_addrlinethree=?, user_city=?, user_state=?, user_country=?, user_pincode=? where user_id=?";
		int updatecount = 0;
		try {
			con = getDBConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement(edithotelUser);
			ps.setString(1, userBean.getUsertype());
			ps.setString(2,userBean.getUserfirstname());
			ps.setString(3, userBean.getUsermiddlename());
			ps.setString(4, userBean.getUserlastname());
			ps.setString(5, userBean.getUseremailid());
			ps.setInt(6, userBean.getUsermobileno());
			ps.setInt(7, userBean.getUserlandline());
			ps.setString(8, userBean.getUserAddresslineone());
			ps.setString(9, userBean.getUserAddresslinetwo());
			ps.setString(10, userBean.getUserAddresslinethree());
			ps.setString(11, userBean.getUsercity());
			ps.setString(12, userBean.getUserstate());
			ps.setString(13, userBean.getUsercountry());
			ps.setInt(14, userBean.getUserpincode());
			ps.setInt(15, userBean.getUserid());
			updatecount = ps.executeUpdate();
			con.commit();
		} catch (Exception e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return updatecount;
	}
	
	public int deleteUser(int userId) {
		Connection con = null;
		PreparedStatement ps = null;
		String edithotelUser = "delete from hotel_user_table where user_id=?";
		int updatecount = 0;
		try {
			con = getDBConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement(edithotelUser);
			ps.setInt(1, userId);
			updatecount = ps.executeUpdate();
			con.commit();
		} catch (Exception e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (ps != null) {
					ps.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return updatecount;
	}


	public ArrayList<UserTypeBean> getuserTypeList() {
		// TODO Auto-generated method stub
		ArrayList<UserTypeBean> userTypeList=new ArrayList<UserTypeBean>();
		
		return userTypeList;
	}

}
